package com.xunyin.modules.device.rest;

import com.xunyin.modules.aop.ForbidSubmit;
import com.xunyin.modules.device.domain.TDevice;
import com.xunyin.modules.device.service.TDeviceService;
import com.xunyin.modules.device.service.dto.TDeviceQueryCriteria;
import com.xunyin.modules.log.Log;
import com.xunyin.modules.security.security.TokenUtil;
import com.xunyin.modules.security.security.vo.JwtUser;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.data.domain.Pageable;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

/**
 * @Author xunyinyunshang  <3594419785@qq.com>
 * @date 2022-11-8
 */
@AllArgsConstructor
@Api(tags = "共享空间:设备管理")
@RestController
@RequestMapping("api")
public class TDeviceController {

    private final TDeviceService tDeviceService;
    private final TokenUtil tokenUtil;

    @Log("查询设备")
    @ApiOperation("查询设备")
    @GetMapping(value = "/tDevice")
    //@PreAuthorize("@el.check('admin','tShareOrder:list')")
    public ResponseEntity getTDevice(HttpServletRequest request, TDeviceQueryCriteria criteria, Pageable pageable) {

        Set<Integer> deviceTypes = new HashSet<>();
        // 查询大门设备
        if (criteria.getDeviceType() == 0) {
            deviceTypes.add(0);
            deviceTypes.add(4);
            deviceTypes.add(5);
            criteria.setDevicePos(1);
        // 查询房门设备、电源、插座设备
        } else if (criteria.getDeviceType() == 1) {
            deviceTypes.add(1);
            deviceTypes.add(2);
            deviceTypes.add(3);
            deviceTypes.add(4);
            deviceTypes.add(5);
            deviceTypes.add(6);
            criteria.setDevicePos(2);
        }
        criteria.setDeviceType(null);
        criteria.setDeviceTypes(deviceTypes);
        //        if (criteria.getStoreId() == 0) {
//            criteria.setStoreId(null);
//        }
        criteria.setStoreId(null);
        JwtUser user = tokenUtil.getJwtUser(tokenUtil.getToken(request));
        if (!user.getRoleIdList().contains(Long.valueOf(1L))) {
            criteria.setStoreId(user.getStoreIdList());
        }

/*        DeviceOptEnum[] deviceOptListEnum = DeviceOptEnum.values();
        List<ParamDto> deviceOptList = new ArrayList<>();
        for (DeviceOptEnum enumItem : deviceOptListEnum) {
            ParamDto paramDto = new ParamDto();
            paramDto.setKey(String.valueOf(enumItem.getValue()));
            paramDto.setName(String.valueOf(enumItem.getName()));
            paramDto.setValue(String.valueOf(enumItem.getValue()));
            deviceOptList.add(paramDto);
        }*/

        Map<String, Object> queryAll = tDeviceService.queryAll(criteria, pageable);
        return new ResponseEntity<>(queryAll, HttpStatus.OK);
    }

    @ForbidSubmit
    @Log("新增设备")
    @ApiOperation("新增设备")
    @PostMapping(value = "/tDevice")
    //@PreAuthorize("hasAnyRole('admin','YXSYSTEMUSERLEVEL_ALL','YXSYSTEMUSERLEVEL_CREATE')")
    public ResponseEntity<Object> create(@Validated @RequestBody TDevice resources) {
        resources.setStatus(1);
        // 如果是添加门店大门锁设备，则默认设置0为不需要包间id
        if(resources.getDevicePos() != null && resources.getDevicePos()==1){
            resources.setStoreRoomId(0);
        }

        //if (resources.getDeviceType() == 0) {
            //resources.setStoreRoomId(0);
        //}
        return new ResponseEntity<>(tDeviceService.save(resources), HttpStatus.CREATED);
    }

    @ForbidSubmit
    @Log("修改设备")
    @ApiOperation("修改设备")
    @PutMapping(value = "/tDevice")
    //@PreAuthorize("hasAnyRole('admin','YXSYSTEMUSERLEVEL_ALL','YXSYSTEMUSERLEVEL_EDIT')")
    public ResponseEntity<Object> update(@Validated @RequestBody TDevice resources) {
        tDeviceService.updateById(resources);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }
}
